{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe161674-e8b6-41e1-b0a0-ca0fa241b4d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ch03-4 Read Alignment "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86f2be45-730f-49c0-8eb2-bf6af68d2456",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Libraries\n",
    "from Bio import pairwise2\n",
    "from Bio.pairwise2 import format_alignment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aabb521e-fa09-4904-92df-05eeef0367ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example using Pairwise2\n",
    "# Define the sequences\n",
    "seq1 = \"ACGTGCTAGCTAG\"\n",
    "seq2 = \"ACGTCGATGCTA\"\n",
    "\n",
    "# Perform global alignment\n",
    "alignments = pairwise2.align.globalxx(seq1, seq2)\n",
    "\n",
    "# Display the best alignment\n",
    "print(\"Best alignment:\")\n",
    "print(format_alignment(*alignments[0]))\n",
    "\n",
    "# Perform local alignment\n",
    "local_alignments = pairwise2.align.localxx(seq1, seq2)\n",
    "\n",
    "# Display the best local alignment\n",
    "print(\"\\nBest local alignment:\")\n",
    "print(format_alignment(*local_alignments[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbf817f0-ed49-4952-8bcd-04c8ba96812d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example with PairwiseAligner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8cd0758-a2b5-4206-9a3f-6439db557f49",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Libraries\n",
    "from Bio.Align import PairwiseAligner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b07b39ea-e3e4-4571-a4af-9ddf07516ead",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the sequences\n",
    "seq1 = \"ACGTGCTAGCTAG\"\n",
    "seq2 = \"ACGTCGATGCTA\"\n",
    "\n",
    "# Initialize the PairwiseAligner\n",
    "aligner = PairwiseAligner()\n",
    "\n",
    "# Set alignment scoring (optional, defaults to match=1, mismatch=0, gap=-1)\n",
    "aligner.match_score = 1\n",
    "aligner.mismatch_score = -1\n",
    "aligner.open_gap_score = -1\n",
    "aligner.extend_gap_score = -0.5\n",
    "\n",
    "# Perform global alignment\n",
    "global_alignments = aligner.align(seq1, seq2)\n",
    "\n",
    "# Display the best global alignment\n",
    "print(\"Best global alignment:\")\n",
    "print(global_alignments[0])\n",
    "print(f\"Score: {global_alignments[0].score}\")\n",
    "\n",
    "# Perform local alignment\n",
    "aligner.mode = 'local'  # Switch to local alignment mode\n",
    "local_alignments = aligner.align(seq1, seq2)\n",
    "\n",
    "# Display the best local alignment\n",
    "print(\"\\nBest local alignment:\")\n",
    "print(local_alignments[0])\n",
    "print(f\"Score: {local_alignments[0].score}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d6b9d0f-a0a0-4f8e-a65f-9efffc636abe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c325b1fb-c3f5-44f9-b87d-fef313281353",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "166103c3-5096-4449-830c-ab49988a0981",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
